package com.j180.demo.service.student.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.j180.demo.bean.PageBean;
import com.j180.demo.bean.StudentBean;
import com.j180.demo.dao.student.IStudentDao;
import com.j180.demo.service.student.IStudentService;
import org.apache.ibatis.annotations.ResultMap;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class StudentServiceImpl extends ServiceImpl<IStudentDao, StudentBean> implements IStudentService {

    @Resource
    private IStudentDao iStudentDao;

    @Override
    public PageBean<List<StudentBean>> showStudengInfoByCondition(Map<String, String> condition) {


        PageBean<List<StudentBean>> pageBean = new PageBean<>();

        String curpage = condition.get("currentPage");
        String pageSize = condition.get("pageSize");
        String studentName = condition.get("studentName");
        String studentGender = condition.get("studentGender");
        String className = condition.get("className");
       // 创建分页对象
        Page pageRequest = new Page(Integer.parseInt(curpage),Integer.parseInt(pageSize));
        //条件构造器
        QueryWrapper queryWrapper = new QueryWrapper();

        if(!"".equals(studentName)&&studentName!=null){
            queryWrapper.like("stu.name",studentName);
        }
        if(!"".equals(studentGender)&&studentGender!=null){
            queryWrapper.eq("stu.gender",studentGender);

        }
        if(!"".equals(className)&&className!=null){
            queryWrapper.like("cls.name",className);
        }
        queryWrapper.eq("isdeleted",0);

        queryWrapper.orderByDesc("stu.id");


        Page<StudentBean> page = iStudentDao.getAllStudentsByPage(pageRequest, queryWrapper);

     // 封装分页对象
        pageBean.setCurPage(page.getCurrent());
        pageBean.setPageSize(page.getSize());
        pageBean.setTolNums(page.getTotal());
        pageBean.setTolPages(page.getPages());
        pageBean.setDatas(page.getRecords());

        return pageBean;
    }

    @Override
    public int addStudentInfo(StudentBean studentBean) {

        return iStudentDao.insertStudent(studentBean);
    }

    @Override
    public int deleteStudentInfo(Integer stduentId) {

        return iStudentDao.deleteById(stduentId);
    }

    @Override
    public int updateStudengInfo(StudentBean studentBean) {

        return iStudentDao.updateStudent(studentBean);
    }

    @Override
    public StudentBean showStudentInfoDetail(Integer studentId) {

        return  iStudentDao.getAllStudentsById(studentId);
    }
}
